﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web;
using System.Web.Mvc;
using CQIE.TES.IService;
using CQIE.TES.Model;
using CQIE.TES.Service;
using CQIE.TES.WEB.USER.Filter;
using CQIE.TES.WEB.USER.Models;
using Newtonsoft.Json.Linq;

namespace CQIE.TES.WEB.USER.Controllers.Eva
{
    public class EvaluatingController : Controller
    {
        public IEvaluaHeightService EvaluaHeightService { get; set; }
        public IQuestionService QuestionService { get; set; }
        public ITeacherService TeacherService { get; set; }
        public ITeachingOfficeService TeachingOfficeService { get; set; }
        public IStudentService StudentService { get; set; }
        // GET: Evaluating
        [LoginFilter]
        public ActionResult StudentEvaIndex(int sid)
        {
            var evaList = EvaluaHeightService.GetEvaluaHeightsByRole(ROLE_ENUM.Student);
            ViewBag.EvaList = evaList;
            if (evaList.Count == 0)
            {
                return View();
            }

            ViewBag.sid = sid;
            Student stu = StudentService.Get(sid);
            ViewBag.UserName = stu.Name;
            Question model = QuestionService.GetqQuestion(ENUM_EVA_TYPE.Student);

            var data = model.Children.Select(r => new Question_Option()
            {
                ID = r.ID,
                Info = r.Info,
                Options = JArray.Parse(r.Options).ToObject<List<Option>>()
            });

            ViewBag.Question = data;
            ViewBag.Num = data.Count();

            return View();
        }



        /// <summary>
        /// 教师评
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        [LoginFilter]
        public ActionResult TeacherEvaIndex(int tid)
        {
            Teacher User = TeacherService.FindByID(tid);
            ViewBag.UserName = User.Name;
            ViewBag.Role = User.Role;
            ViewBag.tid = tid;
            {
                //获取评教列表
                var evaList = EvaluaHeightService.GetEvaluaHeightsByRole(ROLE_ENUM.Self);
                ViewBag.EvaList = evaList;
                if (evaList.Count == 0)
                {
                    return View();
                }
                //获取问卷(教师自评问卷数据)
                Question q = QuestionService.GetqQuestion(ENUM_EVA_TYPE.Self);
                List<Question> data = new List<Question>();

                //遍历问卷
                getChirden(q, data);
                //组装前端需要展示的数据
                var showData = data.Select(r => new Question_Option()
                {
                    ID = r.ID,
                    Info = r.Info,
                    //组装json 数据
                    Options = JArray.Parse(r.Options).ToObject<List<Option>>()

                });
                ViewBag.showData = showData;
                ViewBag.Count = showData.Count();
            }

            {
                //-----------------------------------系部----------------------------------
                // 获取评测列表
                var evaList = EvaluaHeightService.GetEvaluaHeightsByRole(ROLE_ENUM.Department);
                ViewBag.DEvaList = evaList;

                // 获取系部教师列表
                ViewBag.TeacherList = TeacherService.GetDepartMentTeachers(tid);

                // 获取问卷
                Question q = QuestionService.GetqQuestion(ENUM_EVA_TYPE.Department);
                List<Question> data = new List<Question>();

                getChirden(q, data);
                var temp = data.Select(r => new Question_Option()
                {
                    ID = r.ID,
                    Info = r.Info,
                    //将r.Options转换成JArray的json数组在转换成对象，类型是泛型List<Option>
                    Options = JArray.Parse(r.Options).ToObject<List<Option>>()
                });
                ViewBag.Questions = temp;
                ViewBag.DepartmentCount = temp.Count();
            }

            {
                //-----------------------------------互评----------------------------------
                //1、根据角色获取评教列表
                var EvaList = EvaluaHeightService.GetEvaluaHeightsByRole(ROLE_ENUM.Teacher);
                //2、将评教列表传给前端
                ViewBag.EvaList1 = EvaList;
                //3、获取教师互评的系部教师列表
                var teachersList = TeacherService.GetDepartMentTeachers(tid);
                //4、将系部教师列表传递给前端
                ViewBag.teachersList = teachersList;
                //5、获取问卷（教师互评问卷）
                Question q = QuestionService.GetqQuestion(ENUM_EVA_TYPE.Teacher);
                //6、存放问卷(引用类型data)
                List<Question> data = new List<Question>();
                //7、通过递归循环遍历所有叶节点问题
                getChirden(q, data);
                //8、将遍历好的数据重新组装成对象
                var newData = data.Select(r => new Question_Option()
                {
                    ID = r.ID,
                    Info = r.Info,
                    //将r.Options转换成JArray的json数组在转换成对象，类型是泛型List<Option>
                    Options = JArray.Parse(r.Options).ToObject<List<Option>>()
                }).ToList();
                //9、向前端传递数据
                ViewBag.newData = newData;
                //10、问题总数传递给前端
                ViewBag.Count1 = newData.Count;
            }

            {
                //-----------------------------------教研室----------------------------------
                //1、根据角色获取评教列表
                var EvaList = EvaluaHeightService.GetEvaluaHeightsByRole(ROLE_ENUM.Leader);
                ViewBag.EvaListTLeader = EvaList;
                // 获取教研室教师
                List<Teacher> teachers = TeachingOfficeService.GetTeachingOfficeTeacherListByTeacherId(tid);
                
                ViewBag.TteachersList = teachers;
                Question q = QuestionService.GetqQuestion(ENUM_EVA_TYPE.CourseLeader);
                List<Question> data = new List<Question>();
                // 通过递归循环遍历所有叶节点问题
                getChirden(q, data);
                // 将遍历好的数据重新组装成对象
                var res = data.Select(r => new Question_Option()
                {
                    ID = r.ID,
                    Info = r.Info,
                    Options = JArray.Parse(r.Options).ToObject<List<Option>>()
                }).ToList();

                ViewBag.TOdata = res;
                ViewBag.TOCount = res.Count;
            }
            return View();
        }

        

        /// <summary>
        /// 遍历获取叶节点--问题
        /// </summary>
        /// <param name="question"></param>
        /// <param name="list"></param>
        private void getChirden(Question question, List<Question> list)
        {
            if (question.Children.Count>0)
            {
                foreach (Question child in question.Children)
                {
                    getChirden(child,list);
                }
                
            }
            else
            {
                list.Add(question);
            }
        }
    }
}